/*
 * Copyright 2010 Peter Backx, streamhead.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hibari.server.ipn;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class IPNMessage implements Serializable {

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	private Date date = new Date();
	private boolean validated = false;
	private Text fullMessage;
	private TransactionType transactionType;
	private PaymentStatus paymentStatus;
	private String mcGross;
	private String mcCurrency;
	private String custom;
	private String itemNumber;
	private String payerEmail;

	public String getPayerEmail() {
		return this.payerEmail;
	}

	private IPNMessage() {
	}

	public Date getDate() {
		return this.date;
	}

	public Text getFullMessage() {
		return this.fullMessage == null ? new Text("") : this.fullMessage;
	}

	public TransactionType getTransactionType() {
		return this.transactionType;
	}

	public boolean isValidated() {
		return this.validated;
	}

	public PaymentStatus getPaymentStatus() {
		return this.paymentStatus;
	}

	public String getMcGross() {
		return this.mcGross;
	}

	public String getMcCurrency() {
		return this.mcCurrency;
	}

	public String getCustom() {
		return this.custom;
	}

	public String getItemNumber() {
		return this.itemNumber;
	}

	public static class Builder {
		private IPNMessage message = new IPNMessage();

		public Builder(Map<String, String> nvp) {
			StringBuffer msg = new StringBuffer();
			for (Map.Entry<String, String> entry : nvp.entrySet()) {
				msg.append(entry.getKey());
				msg.append("=");
				msg.append(entry.getValue());
				msg.append("\n");
			}
			this.message.fullMessage = new Text(msg.toString());
		}

		public Builder transactionType(TransactionType transactionType) {
			this.message.transactionType = transactionType;
			return this;
		}

		public Builder paymentStatus(PaymentStatus paymentStatus) {
			this.message.paymentStatus = paymentStatus;
			return this;
		}

		public Builder mcGross(String mcGross) {
			this.message.mcGross = mcGross;
			return this;
		}

		public Builder mcCurrency(String mcCurrency) {
			this.message.mcCurrency = mcCurrency;
			return this;
		}

		public Builder validated() {
			this.message.validated = true;
			return this;
		}

		public Builder custom(String value) {
			this.message.custom = value;
			return this;
		}

		public Builder itemNumber(String itemNumber) {
			this.message.itemNumber = itemNumber;
			return this;
		}

		public Builder payerEmail(String email) {
			this.message.payerEmail = email;
			return this;
		}

		public IPNMessage build() {
			return this.message;
		}
	}

	@Override
	public String toString() {
		return "IPNMessage [transactionType=" + this.transactionType + "]";
	}

	public Long getId() {
		return this.id;
	}
}
